package com.bjpowernode.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowernode.boss.constants.BoosConst;
import com.bjpowernode.boss.convert.PayWayConvert;
import com.bjpowernode.boss.domain.PayWay;
import com.bjpowernode.boss.enums.ajax.Code;
import com.bjpowernode.boss.mvc.model.dto.PageInfoDto;
import com.bjpowernode.boss.mvc.model.query.PayWayQuery;
import com.bjpowernode.boss.mvc.model.result.AjaxResult;
import com.bjpowernode.boss.mvc.model.vo.DitOptionVo;
import com.bjpowernode.boss.mvc.service.PayWayService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付方式 控制类
 */
@RestController
@Slf4j
public class PayWayController {

    @Resource
    private PayWayService payWayService;

    @Resource
    private PayWayConvert payWayConvert;

    @GetMapping("/payway/loadWayCodeAndNameOption")
    public AjaxResult loadPayWayOption() {
        log.info("加载支付方式的字典集code 和 name");
        List<DitOptionVo> ditOptionVoList = payWayService.loadPayWayOption();
        return AjaxResult.ok(ditOptionVoList);
    }

    @GetMapping("/payway/loadTypeCodeAndNameOption")
    public AjaxResult loadPayTypeOption(@RequestParam String wayCode) {
        log.info("要查询那种支付方式的类型 : {}", wayCode);
        List<DitOptionVo> ditOptionVoList = payWayService.loadPayTypeOption(wayCode);
        return AjaxResult.ok(ditOptionVoList);
    }

    @PostMapping("/payway/add")
    public AjaxResult addPayWayConfig(@Validated @RequestBody PayWayQuery payWayQuery) {
        log.info("要添加的支付方式是 : {}", payWayQuery);
        payWayService.addPayWayConfig(payWayQuery);
        return AjaxResult.ok();
    }

    @GetMapping("/payWay/pageList")
    public AjaxResult loadWayTablePageData(
            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(required = false) String productCode) {
        log.info("查询支付方式的分页 : {} , 存在的条件查询 : {}", pageNo, productCode);
        IPage<PayWay> pageResultInfo = payWayService.loadWayTablePageData(pageNo, BoosConst.PAGE_SIZE, productCode);
        PageInfoDto pageInfoDto = new PageInfoDto(pageResultInfo.getCurrent(), pageResultInfo.getPages());
        List<PayWay> payWayList = pageResultInfo.getRecords();
        Map<String, Object> data = new HashMap<>();
        data.put("page", pageInfoDto);
        data.put("list", payWayConvert.toConvertPayWayVoList(payWayList));
        return AjaxResult.ok(data);
    }

    @GetMapping("/payWay/remove")
    public AjaxResult removePayWayConfig(@RequestParam String productCode,
                                         @RequestParam String wayCode,
                                         @RequestParam String typeCode) {
        log.info("要删除的支付方式配置所需要的信息如下productCode : {} , wayCode : {} , typeCode : {} " , productCode , wayCode  , typeCode);
      // 判空
        if (StrUtil.isNotBlank(productCode) && StrUtil.isNotBlank(wayCode) && StrUtil.isNotBlank(typeCode)) {
            payWayService.removePayWayConfig(productCode , wayCode , typeCode);
            return AjaxResult.ok();
        }
        return AjaxResult.fail(Code.REQUEST_PARAM_ERR);

    }
}
